<?php

namespace app\admin\controller\bottle;

use app\common\controller\Backend;
use think\Controller;
use think\Request;
use think\Db;
/**
 * 
 *
 * @icon fa fa-circle-o
 */
class Collect extends Backend
{
    /**
     * BottleCollect模型对象
     */
    protected $model = null;
    public function _initialize()
    {
        parent::_initialize();
        $this->model = model('BottleCollect');

    }
    /**
     * 查看
     */
    public function index()
    {
        //设置过滤方法
        $this->request->filter(['strip_tags']);
        if ($this->request->isAjax())
        {
            //如果发送的来源是Selectpage，则转发到Selectpage
            if ($this->request->request('pkey_name'))
            {
                return $this->selectpage();
            }
            list($where, $sort, $order, $offset, $limit) = $this->buildparams();
            $total = $this->model
                ->where($where)
                ->order($sort, $order)
                ->count();

            $list = $this->model
                ->where($where)
                ->order($sort, $order)
                ->limit($offset, $limit)
                ->select();
            $data=array('1'=>'审核中','2'=>'已通过','3'=>'未通过');
            foreach($list as &$l){
                $l['status']=$data[$l['status']];
            }
            $result = array("total" => $total, "rows" => $list);

            return json($result);
        }
        $btns = array('refresh');
        $this->view->assign('btns', $btns);
        return $this->view->fetch();
    }
    public function lists($ids){
        $this->view->assign('id', $ids);
        return $this->view->fetch();
    }
    public function modify(){

        $input=input();

        $ids=$input['ids'];
        $status=$input['status'];
        $content=$input['reamrk'];
        if(!$ids){
            return json(array('code'=>0,'msg'=>'缺少参数'));
        }

        $random_num=$this->model->where('id='.$ids)->find();

        $ramdom=DB::name('bottle_random_num')->where('random_id='.$random_num['random_num'])->find();

        if($random_num['status']!=1){
            return json(array('code'=>0,'msg'=>'该状态不可更改'));
        }
        if($status==2){
            if(empty($ramdom)){
                return json(array('code'=>0,'msg'=>'该码不存在'));
            }
            if($ramdom['is_used']<3){
                return json(array('code'=>0,'msg'=>'该码尚未发放'));
            }
            if($ramdom['is_used']==4){
                return json(array('code'=>0,'msg'=>'该码已使用'));
            }
            $aa=$random_num->save(['status'=>$status,'reamrk'=>$content,'verify_time'=>time()]);
            if($aa<1){
                return json(array('code'=>0,'msg'=>'该码修改失败'));
            }
            $cc=DB::name('BottleRandomNum')->where('random_id='.$random_num['random_num'])->update(['is_used'=>4]);
            if($cc<1){
                return json(array('code'=>0,'msg'=>'该码修改失败2'));
            }
            return json(array('code'=>1,'msg'=>'该码修改成功'));
        }else{

            $aa=$random_num->save(['status'=>$status,'reamrk'=>$content,'verify_time'=>time()]);
            if($aa<1){
                return json(array('code'=>0,'msg'=>'该码修改失败'));
            }
            return json(array('code'=>1,'msg'=>'已拒绝'));
        }
    }
    public function t_ajax(){
        return array('code'=>1,'msg'=>'OK');
    }
}
